package com.dlut.java3;

import java.util.Arrays;
import java.util.Scanner;

/**
 * @Author: LiuHeng
 * @Description:
 * @Date: 2022-09-15 18:45
 */
public class T0916_HuaWei {

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        String sn = in.nextLine();
        int n = Integer.parseInt(sn);
        int[] goods = new int[n];
        String sn2 = in.nextLine();
        String[] ssn = sn2.split(" ");
        for (int i = 0; i < n; i++) {
            goods[i] = Integer.parseInt(ssn[i]);
        }

        String sm = in.nextLine();
        int m = Integer.parseInt(sm);
        int[][] sup = new int[m][];
        for (int i = 0; i < m; i++) {
            String temp = in.nextLine();
            String[] ssm = temp.split(" ");
            int k = ssm.length;
            sup[i] = new int[k];
            for (int j = 0; j < k; j++) {
                sup[i][j] = Integer.parseInt(ssm[j]);
            }
        }

        Arrays.sort(goods);
        for (int i = 0; i < m; i++) {
            Arrays.sort(sup[i]);
        }
        int ans = Integer.MAX_VALUE;
        for (int i = 0; i < m; i++) {
            if (goods[n-1] > sup[i][sup[i].length - 1])
                continue;
            int count = 0;
            for (int j = 0; j < n; j++) {
                count += search(sup[i], goods[j]) - goods[j];
            }
            ans = Math.min(ans, count);
        }
        if (ans == Integer.MAX_VALUE){
            System.out.println(-1);
        }else {
            System.out.println(ans);
        }

    }

    private static int search(int[] ints, int good) {
        int left = 0, right = ints.length - 1;
        int mid = left + (right - left) / 2;
        while (left <= right){
            mid = left + (right - left) / 2;
            if (good > ints[mid]){
                left = mid + 1;
            }else {
                right = mid - 1;
            }
        }
        return ints[left];
    }


}
